package com.javacasino.casino.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.javacasino.casino.entity.BeginGame;
import com.javacasino.casino.entity.OnLoadInfo;
import com.javacasino.casino.entity.OnLogiResponse;
import com.javacasino.casino.entity.login.Master;
import com.javacasino.casino.service.impl.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;


import java.net.URI;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

@Slf4j
public class CustomizedWebSocketClient  extends WebSocketClient
{

    @Value("${socketUri}")
    private String webSocketUri;

    @Autowired
    private RedisService iGlobalCache;

    /**
     * 线程安全的Boolean -是否受到消息
     */
    public AtomicBoolean hasMessage = new AtomicBoolean(false);

    /**
     * 线程安全的Boolean -是否已经连接
     */
    private AtomicBoolean hasConnection = new AtomicBoolean(false);

    public CustomizedWebSocketClient(URI serverUri, Draft_6455 draft_6455) {
        super(serverUri);
    }

    /**
     * 打开链接
     * @param serverHandshake
     */
    @Override
    public void onOpen(ServerHandshake serverHandshake) {
        log.info("打开链接");
    }

    /**
     * 收到消息
     * @param s
     */

    @Override
    public void onMessage(String s) {
        log.info("获取信息={}",s);
        if(s.contains("onLogin")){
            Map mapTypes = JSON.parseObject(s);
            Map result = JSON.parseObject(mapTypes.get("result").toString());
            if((boolean)result.get("event")){
                OnLogiResponse data = JSONObject.parseObject(result.get("data").toString(), OnLogiResponse.class);
                iGlobalCache.setCacheObject(String.valueOf(data.getHallID()),data);
//                MapStaticConstants.objectMap.put(data.getSid(),data);
                log.info("获取信息={}",data);
            }
        }else if (s.contains("onOnLoadInfo2")){
            //获取登录账号信息
            Map mapTypes = JSON.parseObject(s);
            Map result = JSON.parseObject(mapTypes.get("result").toString());
            if((boolean)result.get("event")){
                OnLoadInfo data = JSONObject.parseObject(result.get("data").toString(), OnLoadInfo.class);
                iGlobalCache.setCacheObject(data.getHallID(),data);
//                MapStaticConstants.objectMap.put(data.getHallID(),data);
                log.info("获取信息={}",data);
            }
            Collection<String> collections = iGlobalCache.keys("master_sid_info");

        }else if (s.contains("onBeginGame")){
            //获取订单号
            Map mapTypes = JSON.parseObject(s);
            Map result = JSON.parseObject(mapTypes.get("result").toString());
            if((boolean)result.get("event")){
                BeginGame data = JSONObject.parseObject(result.get("data").toString(), BeginGame.class);
                iGlobalCache.setCacheObject(String.valueOf(data.getBBJackpot().getPools().get(1).getPoolID()),data);
//                MapStaticConstants.objectMap.put("wagersID",data.getWagersID());
                log.info("获取信息={}",data);
            }

//        }else if (s.contains("onTakeMachine")){
//        }else if (s.contains("updateMarquee")){
        }
        hasMessage.set(true);
    }

//    public Class jsonToEntity(String s, Class<T> clazz){
//        Map mapTypes = JSON.parseObject(s);
//        Map result = JSON.parseObject(mapTypes.get("result").toString());
//        if((boolean)result.get("event")){
//            clazz = JSONObject.parseObject(result.get("data").toString(), clazz);
//            log.info("获取信息={}",clazz);
//        }
//        return clazz;
//    }



    /**
     * 关闭链接
     * @param i
     * @param s
     * @param b
     */
    @Override
    public void onClose(int i, String s, boolean b) {
        super.close();
        log.info("关闭链接={}",s);
        this.hasMessage.set(false);
        this.hasConnection.set(false);
    }

    /**
     * 发生错误
     * @param e
     */
    @Override
    public void onError(Exception e) {

    }

    @Override
    public void connect(){
        if(!this.hasConnection.get()){
            super.connect();
            hasConnection.set(true);
        }
    }

}
